<template>
  <div class="search">
    <Row>
      <Col>
        <Card>
          <Row class="operation">
            <Form inline :label-width="170" ref="form" :model="form" label-position="right">
              <Button @click="getDataList" icon="md-refresh">刷新</Button>
              <Button @click="add" type="primary" icon="md-add">添加</Button>

              <!-- <Form-item label="广告类型" prop="advertisingType">
                <Select v-model="form.advertisingType" style="width:200%">
                  <Option value="1">开屏</Option>
                  <Option value="2">轮播</Option>
                </Select>
              </Form-item> -->
              <FormItem label="账号类型" prop="advertisingJumpType">
                <Select v-model="listtype" style="width:200%">
                  <Option value="1">个人商家</Option>
                  <Option value="2">普通用户</Option>
                  <Option value="3">管理员</Option>
                  <Option value="4">商家</Option>
                </Select>
              </FormItem>
              <!-- <FormItem> -->
                <Button @click="handleSearch" type="primary" icon="ios-search" style="margin-left:90px;">确定</Button>
                <!-- <Button @click="delAll" icon="md-trash">删除</Button> -->
              <!-- </FormItem> -->
            </Form>
            <!-- <Button @click="add" type="primary" icon="md-add">添加</Button>
            <Button @click="delAll" icon="md-trash">删除</Button>
            <Button @click="getDataList" icon="md-refresh">刷新</Button> -->
          </Row>
          <Row class="operation">
                <Form inline :label-width="50" ref="form" :model="userform" label-position="right">
                    <FormItem label="手机号" prop="mobile" >
                        <input type="text" v-model="userform.mobile"/>
                    </FormItem>
                    <FormItem label="姓名" prop="username" >
                        <input type="text" v-model="userform.username"/>
                    </FormItem>
                    <Button @click="userSearch" type="primary" icon="ios-search" style="margin-left:50px;">获取用户列表</Button>
                </Form>
          </Row>
          <Row>
            <Alert show-icon>
              已选择 <span class="select-count">{{selectCount}}</span> 项
              <a class="select-clear" @click="clearSelectAll">清空</a>
            </Alert> 
          </Row>
          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>
          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
      </Col>
    </Row>
    <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="700">
      <Form ref="form" :model="form" :label-width="100" :rules="formValidate" >
        <FormItem label="用户名" prop="username">
          <Input v-model="form.username" autocomplete="off"/>
        </FormItem>
        <FormItem label="密码" prop="password" v-if="modalType==0">
          <Input type="password" v-model="form.password" autocomplete="off"/>
        </FormItem>
        <!-- <FormItem label="邮箱" prop="email">
          <Input v-model="userForm.email"/>
        </FormItem> -->
        <FormItem label="手机号" prop="mobile">
          <Input v-model="form.mobile"/>
        </FormItem>
        <FormItem label="性别" prop="sex">
          <Select v-model="form.sex" placeholder="请选择">
            <Option value="男">男</Option>
            <Option value="女">女</Option>
          </Select>
        </FormItem>
         <FormItem label="地址" prop="address">
          <Input v-model="form.address"/>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="modalVisible=false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
      </div>
    </Modal>
    
  </div>
</template>

<script>
import {
  getUserListData,
} from "@/api/index";
import circleLoading from "@/views/my-components/circle-loading.vue";
import uploadPicInput from "@/views/my-components/xboot/upload-pic-input.vue"
// import departmentTreeChoose from "@/views/my-components/xboot/department-tree-choose";
export default {
  name: "staffManagement",
  components: {
    circleLoading,
    uploadPicInput
    // departmentTreeChoose
  },
  data() {
    return {
        listtype:'',
      loading: true, // 表单加载状态
      loadingEdit: false,
      modalType: 0, // 添加或编辑标识
      modalVisible: false, // 添加或编辑显示
      modalTitle: "", // 添加或编辑标题
      searchForm: { // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        // advertisingType: "",
        // advertisingJumpType: ""
      },
      userform:{
       mobile:'',
       username:'',
       pageNumber:1,
       pageSize:10,
      },
      form: { // 添加或编辑表单对象初始化数据
        username:'',
        password:'',
        mobile:'',
        sex:'',
        address:'',
      },
      selectData:[],
      // 表单验证规则
      formValidate: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        classifyName: [{ required: true, message: "不能为空", trigger: "blur" }]
      },
      submitLoading: false, // 添加或编辑提交状态
    //   selectList: [], // 多选数据
      selectCount: 0, // 多选计数
      columns: [
        // 表头
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "头像",
          key: "avatar",
          minWidth: 120,
          sortable: false,
          render: (h, params) => {
            return h("avatar", {
              props: {
                src: params.row.avatar
              }
            });
          }
          
        },
        {
          title: "姓名",
          key: "username",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "手机号码",
          key: "mobile",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "性别",
          key: "sex",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "地址",
          key: "address",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          minWidth: 400,
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                    icon: "ios-create-outline",
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.row);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small",
                    icon: "md-trash"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.remove(params.row);
                    }
                  }
                },
                "删除"
              ),
               h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                    disabled: (params.row.status == -1),
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.frozen(params.row);
                    }
                  }
                },
                "冻结"
              ),
            ]);
          }
        }
      ],
      data: [], // 表单数据
      classifyData: [],
      brandId: "",//删除选中的品牌id
    //   jobdata:[],
    //   detaildata:{},
      type:"",
      total: 0 // 表单数据总数
    };
  },
  methods: {
    init() {
      this.getDataList();
      // this.getClassifyList();
    },
    //获取用户列表
    userSearch(){
      this.postRequest("/user/getUserList",this.userform).then(res =>{
          console.log(res)
          if(res.success){
              this.data = res.result.data
          }
          else{
              this.$Message.success("暂无数据");
          }
      })
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    handleSearch() {
    if(this.listtype == 1){
        this.data = []
        this.postRequest("/user/getBackUserPersonStoreList",this.searchForm).then(res =>{
               console.log(res)
               if(res.success){
                   this.data = res.result.data
               }
           }) 
    }
    else if(this.listtype == 2){
          this.data = []
        this.postRequest("/user/getBackUserCommonList",this.searchForm).then(res =>{
               console.log(res)
               if(res.success){
                   this.data = res.result.data
               }
           })
    }
    else if(this.listtype == 3){
          this.data = []
        this.postRequest("/user/getBackUserAdminList",this.searchForm).then(res =>{
               console.log(res)
               if(res.success){
                   this.data = res.result.data
               }
           })
    }
    else if(this.listtype == 4){
          this.data = []
        this.postRequest("/user/getBackUserStoreList",this.searchForm).then(res =>{
               console.log(res)
               if(res.success){
                   this.data = res.result.data
               }
           }) 
    }
    },
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      // 重新加载数据
      this.getDataList();
    },
    //   handleSelectDepTree(v) {
     
    //   this.form.departmentId = v[0];
    // },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order === "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    clearSelectAll() {
      this.listtype = ''
      this.userform.username=''
      this.userform.mobile=''
    },
    getDataList() {
        this.loading = true;
        // 带多条件搜索参数获取表单数据 请自行修改接口
        this.postRequest("/user/getBackUserAdminList", this.searchForm).then(res => {
          console.log(res)
            this.loading = false;
            if (res.success) {
              this.data = res.result.data;
              this.total = res.result.size;
            } else if (res.code == 201) {
              this.data=[];
              this.total=0;
            }
        });
      // 以下为模拟数据
    //   var params={
    //       pageNumber:1,
    //       pageSize:100
    //   }
    //      this.postRequest("/job/getJobList", params).then(res => {
    //     this.loading = false;
    //     if (res.success) {
    //       this.jobdata = res.result.data;
      
    //     }
    //   });
    },
    getClassifyList() {
        var data = {
            pid: ""
        }
        this.postRequest("/goodCategory/getGoodCategoryList",data).then(res => {
            this.loading = false;
            if (res.success) {
                res.result.forEach(function(e) {
                  e.title=e.title;
                  if (e.isParent) {
                    e.loading = false;
                    e.children = [];
                  }
                });
              this.classifyData = res.result;
            }
        });
    },
    loadData(item, callback) { 
        // 异步加载树子节点数据
      var param ={
          pid:item.id
      }
      this.postRequest("/goodCategory/getGoodCategoryList",param).then(res => {
        if (res.success) {
          res.result.forEach(function(e) {
            e.title=e.title
            if (e.isParent) {
              e.loading = false;
              e.children = [];
            }
          });
          callback(res.result);
        }
      });
    },
    selectTreeEdit(v){
        this.form.classifyId = v[0].id;
        this.form.classifyName = v[0].title;
        this.$forceUpdate();
    },
    handleSubmit() {

        this.$refs.form.validate(valid => {
            if (valid) {
                this.submitLoading = true;
                if (this.modalType === 0) {
                    console.log(this.form)
                    this.postRequest("/user/addUser", this.form).then(res => {
                        this.submitLoading = false;
                        console.log(res)
                        if (res.success) {
                            this.$Message.success("操作成功");
                            this.getDataList();
                            this.modalVisible = false;
                        }
                    });
                } else {
                  console.log("编辑")
                    this.postRequest("/user", this.form).then(res => {
                        this.submitLoading = false;
                        if (res.success) {
                            this.$Message.success("操作成功");
                            this.getDataList();
                            this.modalVisible = false;
                        }
                    });
                }
            }
        });
    },
    add() {
      this.modalType = 0;
      this.modalTitle = "添加";
      this.$refs.form.resetFields();
      this.modalVisible = true;
    },
    edit(v){
     this.modalType = 1
     this.form.username =v.username
     this.form.mobile = v.mobile
     this.form.sex = v.sex
     this.form.address = v.address
     this.modalVisible = true
     console.log(this.form)
    },
    frozen(v) {
        console.log(v)
        let params ={
            userId:v.id
        }
        this.postRequest("/user/freezeAccount",params).then(res =>{
            console.log(res)
            if(res.success){
               this.$Message.success("冻结成功");
            }
        })
    //   this.modalType = 1;
    //   this.modalTitle = "编辑";
    //   this.$refs.form.resetFields();
    //   // 转换null为""
    //   for (let attr in v) {
    //     if (v[attr] === null) {
    //       v[attr] = "";
    //     }
    //   }
    //   let str = JSON.stringify(v);
    //   let data = JSON.parse(str);
    //   this.form = data;
    //   delete this.form._index;
    //   delete this.form._rowKey;
    //   console.log(this.form)
    //   this.modalVisible = true;
    },
    remove(v){
      this.$Modal.confirm({
        title: "确认删除",
        // 记得确认修改此处
        content: "您确认要删除?",
        loading: true,
        onOk: () => {
          var param={
            ids:v.id
          }
          this.postRequest("/advertising/removeAdvertising",param).then(res => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        }
      });
    },
    delAll() {
        console.log(this.data)
        if (this.selectCount <= 0) {
            this.$Message.warning("您还未选择要删除的数据");
            return;
        }
        this.$Modal.confirm({
            title: "确认删除",
            content: "您确认要删除所选的数据?",
            loading: true,
            onOk: () => {
            // 批量删除
                this.postRequest("/size/removeSize",{id:this.brandId}).then(res => {
                    this.$Modal.remove();
                    if (res.success) {
                        this.$Message.success("操作成功");
                        this.clearSelectAll();
                        this.getDataList();
                    }
                })
            // 模拟请求成功
                this.$Message.success("操作成功");
                this.$Modal.remove();
                this.clearSelectAll();
                this.getDataList();
            }
        })
    }
},
  mounted() {
    this.init();
  }
};
</script>
<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>